package com.xiaoyu.commonlib.network;

import android.text.TextUtils;
import android.util.Log;

import com.xiaoyu.commonlib.CommonPlug;
import com.xiaoyu.commonlib.plug.IMasterPlugRules;
import com.xiaoyu.commonlib.plug.PlugManager;
import com.xiaoyu.commonlib.utils.PreferencesUtil;
import com.xiaoyu.commonlib.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * token过期帮助类
 * Created by jk on 2018/4/10.
 */

public class TokenHelper {

    /**
     * 处理token过期的函数
     */
    public static void handleTokenExpired(final IRequestTokenListener listener){
        PreferencesUtil.commit(PreferencesUtil.TOKEN,"");
        int type = PreferencesUtil.getInt(PreferencesUtil.LOGIN_TYPE,-1);
        switch (type){
            case  1://账号密码登录
                goToLoginActivity();
                /*String username = PreferencesUtil.getString(PreferencesUtil.USERNAME,"");
                String password = PreferencesUtil.getString(PreferencesUtil.PASSWORD,"");
                if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)){
                    *//**
                     * 用户名或密码为空，跳转到登陆界面
                     *//*
                    goToLoginActivity();
                }else {
                    NetworkManager.login(username, password, new CommonHttpResponser() {
                        @Override
                        public void onSuccess(String result) {
                            try {
                                JSONObject jsonObject = new JSONObject(result);
                                JSONObject data = jsonObject.getJSONObject("data");
                                PreferencesUtil.commit(PreferencesUtil.TOKEN, data.getString("token"));//用于判断是否登录
                                Log.i("xiaoxue","重新登录 login sucess TOKEN = "+PreferencesUtil.getString(PreferencesUtil.TOKEN,""));
                                if (listener != null){
                                    listener.onTokenAccess();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }

                        @Override
                        public void onFailed(String errorCode, String errorDiscription) {
                            Log.i("xiaoxue","重新登录 login onFailed errorCode = "+errorCode+"   errorDiscription="+errorDiscription);
                            //重新登录失败，跳转到登录界面
                            goToLoginActivity();
                        }
                    });
                }*/
                break;
            case 2://第三方登录
                goToLoginActivity();
                break;
        }

    }

    private static void goToLoginActivity() {
        ToastUtil.showToast(CommonPlug.getAppContext(),"token过期请重新登录！");
        PlugManager.getInstance().getPlugRulesByType(PlugManager.MASTER_ID,
                IMasterPlugRules.class).goToLoginActivity();
    }

    /**
     * token监听接口
     */
    interface IRequestTokenListener{
        /**
         * 获取token的函数
         */
        void onTokenAccess();
    }

}
